function dataGridResize(selector, timeout) {
    setTimeout(function() {
        $(selector).datagrid('resize', {
            height: $("#layoutCenter").height(),
            width: $("#layoutCenter").width()
        });
    }, timeout);
}


/**
 * Formatter function is to return a brazilian formated string dd/mm/yyyy date
 * @return String
 */
function formatter(date, time) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    var h = date.getHours();
    var M = date.getMinutes();
    var s = date.getSeconds();
    function formatNumber(value) {
        return (value < 10 ? '0' : '') + value;
    }
    return formatNumber(d) + '/' + formatNumber(m) + '/' + y + (time ? (' ' + formatNumber(h) + ':' + formatNumber(M) + ':' + formatNumber(s)) : '');
}

/**
 * Formatter function is to parse the string dd/mm/yyyy or dd-mm-yyyy or yyyy-mm-dd and return a Date object
 * @return Date
 */
function parser(s) {
    if (s) {
        console.log(s);
        var a = s.split('/');
        if (a.length > 1) {//dd/mm/yyyy
            console.log('dd/mm/yyyy');
            var d = new Number(a[0]);
            var m = new Number(a[1]);
            //if there is hour split again
            var a2 = a[2].split(' ');
            var y = new Number(a2[0]);
        } else {
            a = s.split('-');
            if (!a.length > 1) {
                return new Date();
            } else
            if (a[0].length === 4) {//yyyy-mm-dd
//if there is hour split again
                console.log('yyyy-mm-dd');
                var a2 = a[2].split(' ');
                var d = new Number(a2[0]);
                var m = new Number(a[1]);
                var y = new Number(a[0]);
            } else
            if (a[0].length === 2) {//dd-mm-yyyy
//if there is hour split again
                console.log('dd-mm-yyyy');
                var a2 = a[2].split(' ');
                var d = new Number(a[0]);
                var m = new Number(a[1]);
                var y = new Number(a2[0]);
            } else {
                return new Date();
            }
        }
        a = s.split(' ');
        var h = 0;
        var M = 0;
        var s = 0;
        if (a.length > 1) {
            a = a[1].split(':');
            var h = new Number(a[0]);
            var M = new Number(a[1]);
            var s = new Number(a[2]);
        }
        var dd = new Date(y, m - 1, d, h, M, s, 0);
        console.log(dd);
        return dd;
    } else {
        return new Date();
    }
}

function validarcpf(cpf) {
    cpf = cpf.replace(/[^\d]+/g, '');

    if (cpf == '')
        return false;

    // Elimina CPFs invalidos conhecidos
    if (cpf.length != 11 ||
            cpf == "00000000000" ||
            cpf == "11111111111" ||
            cpf == "22222222222" ||
            cpf == "33333333333" ||
            cpf == "44444444444" ||
            cpf == "55555555555" ||
            cpf == "66666666666" ||
            cpf == "77777777777" ||
            cpf == "88888888888" ||
            cpf == "99999999999")
        return false;

    // Valida 1o digito
    add = 0;
    for (i = 0; i < 9; i ++)
        add += parseInt(cpf.charAt(i)) * (10 - i);
    rev = 11 - (add % 11);
    if (rev == 10 || rev == 11)
        rev = 0;
    if (rev != parseInt(cpf.charAt(9)))
        return false;

    // Valida 2o digito
    add = 0;
    for (i = 0; i < 10; i ++)
        add += parseInt(cpf.charAt(i)) * (11 - i);
    rev = 11 - (add % 11);
    if (rev == 10 || rev == 11)
        rev = 0;
    if (rev != parseInt(cpf.charAt(10)))
        return false;

    return true;

}

jQuery.expr[":"].contains = jQuery.expr.createPseudo(function(arg) {
    return function(elem) {
        return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});
function filterList(header, list) {
    var form = $("<form>").attr({"class": "filterform", "action": "#"}),
    input = $("<input>").attr({"class": "filterinput", "type": "text"});
    $(form).append(input).appendTo(header);
    $(input).change(function() {
        console.log(this);
        console.log(list);
        var filter = $(this).val();
        if (filter) {
            filterMatches(list, filter, this)
        } else {
            $(list).find("li").slideDown('fast', function() {
                // Animation complete.
            });
        }
        return false;
    }).keyup(function() {
        $(this).change();
    });
}

var timer;
function filterMatches(list, filter, input) {
    $(input).attr('class', 'filterinputSearching');
    window.clearTimeout(timer);
    timer = window.setTimeout(function() {
        $matches = $(list).find('a:Contains(' + filter + ')').parent();
        if ($matches.length > 0) {
            $('li', list).not($matches).slideUp('fast', function() {
                // Animation complete.
                window.setTimeout(function() {
                    $(input).attr('class', 'filterinput');
                }, 500);
            });
            $matches.slideDown('fast', function() {
                // Animation complete.
                window.setTimeout(function() {
                    $(input).attr('class', 'filterinput');
                }, 500);
            });
        } else {
            $matches = $(list).find('div:Contains(' + filter + ')').parent();
            $('li', list).not($matches).slideUp('fast', function() {
                // Animation complete.
                window.setTimeout(function() {
                    $(input).attr('class', 'filterinput');
                }, 500);
            });
            $matches.slideDown('fast', function() {
                // Animation complete.
                window.setTimeout(function() {
                    $(input).attr('class', 'filterinput');
                }, 500);
            });
        }
    }, 500);
}

function replaceAll(find, replace, str) {
    return str.replace(new RegExp(find, 'g'), replace);
}